package spar.cancer.data.to.dump;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import spar.cancer.struct.CheckInfo;

import java.util.List;
import java.util.Map;

public class DumpRedis extends DumpAbstract {
    private final JedisPool jedisPool;

    public DumpRedis(JedisPoolConfig jedisPoolConfig, String host, int port) {
        jedisPool = new JedisPool(jedisPoolConfig, host, port);
    }

    public DumpRedis(String host, int port) {
        jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
    }

    public void close() {
        jedisPool.close();
    }

    public void dumpChkInfos(List<CheckInfo> checkInfos) {
        Jedis jedis = jedisPool.getResource();
        for (CheckInfo info : checkInfos) {
            String key = info.lineNumber + "-" + info.iterId + "-" + info.checkId + "-" + info.name;
            jedis.hmset(key, makeHash(info));
        }
        jedis.close();
    }

    public void dumpLineCodes(Map<Integer, List<String>> lineCodes) {
        Jedis jedis = jedisPool.getResource();
        lineCodes.forEach((lineNumber, codes) -> {
            String key = lineNumber + "-codes";
            for (String code : codes) {
                jedis.rpush(key, code);
            }
        });
        jedis.close();
    }
}
